package iosr11.Service;

import iosr11.data.Event;
import iosr11.data.EventSeries;
import iosr11.data.Note;
import iosr11.data.ProjectGroup;
import iosr11.gbase.dao.EventDAO;
import iosr11.gbase.dao.NoteDAO;
import iosr11.gbase.dao.ProjectGroupDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class NoteServiceImpl implements NoteService{

	private NoteDAO noteDAO;
	private ProjectGroupDAO projectGroupDAO;
	private EventDAO eventDAO;
	private Logger log = Logger.getLogger("service-log");
	
	public NoteServiceImpl(){
		
	}
	
	@Override
	public boolean addNote(Note note) {
		if (note==null){
			log.info("nie mozna utworzyc notatki - brak notatki");
			return false;
		}
		return noteDAO.add(note);
	}

	@Override
	public boolean createNote(String text, Event event, ProjectGroup group) {
		if (text==null){
			log.info("brak tresci");
			return false;
		}
		if (event==null){
			log.info("brak eventu");
			return false;
		}
		if (group==null){
			log.info("brak grupy");
			return false;
		}
		Note note = new Note();
		note.setText(text);
		note.setEvent(event);
		note.setGroup(group);
		return this.addNote(note);
	}

	@Override
	public boolean deleteNote(Note note) {
		if (note==null){
			log.info("nie podano notatki");
			return false;
		}
		if (note.getId()==null){
			log.info("brak id w notatce");
			return false;
		}
		return noteDAO.delete(note);
	}

	@Override
	public List<Note> getNotesForEvent(Event event) {
		List<Note> notes = noteDAO.getAll();
		if (notes==null){
			log.info("nie znaleziono notatek");
			return null;
		}
		List<Note> result = new ArrayList<Note>();
		
		for (Note tmp : notes){
			if (tmp.getEvent().getId().equals(event.getId())){
				tmp.setEvent(event);
				tmp.setGroup(projectGroupDAO.getByID(tmp.getGroup().getId()));
				result.add(tmp);
			}
		}
		return result;
	}

	@Override
	public List<Note> getGroupNotes(ProjectGroup group, Event event) {
		if (group==null){
			log.info("nie podano grupy - blad");
			return null;
		}
		if (event==null){
			log.info("nie podano eventu - blad");
			return null;
		}
		List<Note> notes = this.getNotesForEvent(event);
		if (notes==null){
			log.info("brak notatek do eventu");
			return null;
		}
		List<Note> result = new ArrayList<Note>();
		for (Note note : notes){
			if (note.getGroup()!=null){
				if (note.getGroup().getId().equals(group.getId())){
					note.setGroup(group);
					result.add(note);
				}
			}
			else {
				log.warning("do notatki nie jest podpieta grupa");
			}
		}
		return result;
	}

	@Override
	public List<Note> getGroupNotes(ProjectGroup group, EventSeries series) {
		if (group==null){
			log.info("nie podano grupy - blad");
			return null;
		}
		if (series.getEvents()==null){
			log.info("brak eventow dla serii - szukam w bazie");
			List<Event> lista = eventDAO.getEventForSeries(series);
			if (lista == null){
				log.info("w bazie brak eventow do serii");
				return null;
			}
			series.setEvents(lista);
		}
		List<Note> result = new ArrayList<Note>();
		for (Event event : series.getEvents()){
			List<Note> tmp = this.getGroupNotes(group, event);
			if (tmp!=null){
				result.addAll(tmp);
			}
		}
		return result;
	}

	@Override
	public Note getNote(String id) {
		Note note = noteDAO.getByID(id);
		if (note==null){
			log.info("nie znaleziono notatki o podanym id");
			return null;
		}
		ProjectGroup group = projectGroupDAO.getByID(note.getGroup().getId());
		note.setGroup(group);
		Event event = eventDAO.getByID(note.getEvent().getId());
		note.setEvent(event);
		return note;
	}

	@Override
	public List<Note> getAll() {
		// TODO Auto-generated method stub
		List<Note> lista = noteDAO.getAll();
		if (lista==null){
			log.info("brak notatek w bazie");
			return null;
		}
		List<Note> result = new ArrayList<Note>();
		for(Note note : lista){
			Note tmp = this.getNote(note.getId());
			result.add(tmp);
		}
		
		return result;
	}

	@Override
	public boolean updateNote(Note note) {
		if (note==null){
			log.info("brak notatki");
			return false;
		}
		if (note.getId()==null){
			log.info("brak id - nie mozna zmienic notatki");
			return false;
		}
		if (note.getEvent()==null){
			log.info("brak eventu - nie mozna zmienic notatki");
			return false;
		}
		if (note.getGroup()==null){
			log.info("brak grupy - nie mozna zmienic notatki");
			return false;
		}
		if (note.getText()==null){
			log.info("brak tresci - nie mozna zmienic notatki");
			return false;
		}
		
		return noteDAO.update(note);
	}

	@Override
	public Class<Note> getServiceEntityClass() {
		return Note.class;
	}
	
	//================================================================
	// settery dla springa
	//================================================================
	
	public void setNoteDAO(NoteDAO noteDAO) {
		this.noteDAO = noteDAO;
	}

	public void setProjectGroupDAO(ProjectGroupDAO projectGroupDAO) {
		this.projectGroupDAO = projectGroupDAO;
	}

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

}
